package com.hjk.common.utils.math;



import lombok.SneakyThrows;
import org.apache.commons.lang3.ObjectUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

public class BigDecimalUtils {
    //保留1位数 12.1%
    private static DecimalFormat df_3;
    //保留0位数 12%
    private static DecimalFormat df_2;
    //格式化千分符 10,122
    private static DecimalFormat micrometerOperator;

    static {
        df_3 = new DecimalFormat("0.0##%");
        df_2 = new DecimalFormat("0.##%");
        micrometerOperator = new DecimalFormat("###,###");
    }

    @SneakyThrows
    public static List<?> setScaleList(List<?> list) {
        if (ObjectUtils.isNotEmpty(list)) {
            list.forEach(BigDecimalUtils::setScale);
        }
        return list;
    }

    @SneakyThrows
    public static void setScale(Object obj) {
        if (ObjectUtils.isEmpty(obj)) {
            return ;
        }

        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            SetScale annotation = field.getAnnotation(SetScale.class);
            if (ObjectUtils.isEmpty(annotation)) {
                continue;
            }
            //保留位数
            int newScale = annotation.newScale();
            //保留位数算法,默认四舍五入
            int roundingMode = annotation.roundingMode();
            //是否格式化百分比 20%
            boolean percentage = annotation.percentage();
            //精度 默认万
            int precision = annotation.precision();

            //获取值
            Object name = field.get(obj);
            if (ObjectUtils.isEmpty(name)) {
                continue;
            }

            BigDecimal temp = null;
            if (name instanceof BigDecimal) {
                temp = (BigDecimal) name;
            } else {
                continue;
            }
            //如果是百分比格式化
            if (percentage) {
                //默认保留3位数
                BigDecimal bigDecimal = temp.setScale(newScale, roundingMode);
                String format = "";
                if (newScale == 2) {
                    format = df_2.format(bigDecimal.doubleValue());
                } else {
                    format = df_3.format(bigDecimal.doubleValue());
                }

                field.set(obj, format);
            } else {
                field.set(obj, temp.setScale(newScale, roundingMode));
            }

            //精确到万
            if (precision != -1) {
                field.set(obj, temp.divide(new BigDecimal(precision), newScale, roundingMode));
            }

        }
    }

    /**
     * 格式化四舍五入
     *
     * @param temp
     * @param newScale 保留位数
     * @return
     */
    public static Object setScale(Object temp, Integer newScale) {
        if (ObjectUtils.isEmpty(temp)) {
            return null;
        }
        return ((BigDecimal)temp).setScale(newScale, BigDecimal.ROUND_HALF_UP);
    }
    /**
     * 格式化四舍五入 保留 0位数据
     *
     * @param temp
     */
    public static Object setScaleAsZero(Object temp) {
        if (ObjectUtils.isEmpty(temp)) {
            return null;
        }
        return ((BigDecimal)temp).setScale(0, BigDecimal.ROUND_HALF_UP);
    }


    /**
     * 百分比格式化
     *
     * @param temp
     * @param newScale
     * @return
     */
    public static Object percentageSetScale(Object temp, Integer newScale) {
        if (ObjectUtils.isEmpty(temp)) {
            return null;
        }
        //默认保留3位数
        BigDecimal bigDecimal = ((BigDecimal)temp).setScale(newScale, BigDecimal.ROUND_HALF_UP);
        String format = "";
        if (newScale == 2) {
            format = df_2.format(bigDecimal.doubleValue());
        } else {
            format = df_3.format(bigDecimal.doubleValue());
        }
        return format;
    }


}
